package com.polaris.live.utils

import android.graphics.Color
import androidx.annotation.IntDef
import com.polaris.live.R
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.resp.bean.UserAttireCacheBean
import java.util.concurrent.ConcurrentHashMap

/**
 * 返回通用财富等级魅力等级图片
 *
 * @author Created by 半仙 on 2023/9/4/004 12:18
 */
object RichCharmImgUtil {

    //返回财富等级通用图片
    fun getCommonRichImg(richLevel: Long): Pair<Int, Int?> {
        var tempRichLevel = -1
        var tempNum = -1

        val richData = getRichData()
        val values = richData.values

        values.forEach {
            if (richLevel >= it) {
                tempNum = it
            }
        }

        richData.forEach { (t, u) ->
            if (tempNum == u) {
                tempRichLevel = t.toInt()
            }
        }

        return getCommonRichImg(tempRichLevel)
    }

    //返回魅力等级通用图片
    fun getCommonCharmImg(charmLevel: Long): Pair<Int, Int?> {
        var tempCharmLevel = -1
        var tempNum = -1

        val charmData = getCharmData()
        val values = charmData.values

        values.forEach {
            if (charmLevel >= it) {
                tempNum = it
            }
        }

        charmData.forEach { (t, u) ->
            if (tempNum == u) {
                tempCharmLevel = t.toInt()
            }
        }

        return getCommonCharmImg(tempCharmLevel)
    }

    //返回财富等级通用图片
    fun getCommonRichImg(richLevel: Int): Pair<Int, Int?> {
        val richImage = when {
            richLevel in 1 until 10 -> R.drawable.level_wealth1
            richLevel in 10 until 21 -> R.drawable.level_wealth1_long
            richLevel in 21 until 41 -> R.drawable.level_wealth10
            richLevel in 41 until 61 -> R.drawable.level_wealth20
            richLevel in 61 until 81 -> R.drawable.level_wealth30
            richLevel in 81 until 100 -> R.drawable.level_wealth40
            richLevel in 100 until 101 -> R.drawable.level_wealth40_long
            richLevel in 101 until 121 -> R.drawable.level_wealth50
            richLevel in 121 until 141 -> R.drawable.level_wealth60
            richLevel in 141 until 161 -> R.drawable.level_wealth70
            richLevel in 161 until 181 -> R.drawable.level_wealth80
            richLevel in 181 until 200 -> R.drawable.level_wealth90
            richLevel >= 200 -> R.drawable.level_wealth100
            else -> null
        }

        return Pair(richLevel, richImage)
    }

    //返回魅力等级通用图片
    fun getCommonCharmImg(charmLevel: Int): Pair<Int, Int?> {
        val charmImage = when {
            charmLevel in 1 until 10 -> R.drawable.level_charm1
            charmLevel in 10 until 21 -> R.drawable.level_charm1_long
            charmLevel in 21 until 41 -> R.drawable.level_charm10
            charmLevel in 41 until 61 -> R.drawable.level_charm20
            charmLevel in 61 until 81 -> R.drawable.level_charm30
            charmLevel in 81 until 100 -> R.drawable.level_charm40
            charmLevel >= 100 -> R.drawable.level_charm40_long
            else -> null
        }
        return Pair(charmLevel, charmImage)
    }

    // pair 的 first 是等级   sencond 是背景图片
    fun getRichCharmInt(sex: Int, rich: Long? = null, charm: Long? = null): Pair<Int, Int?> {
        return if (CommonUtils.isMan(sex)) {
            rich?.let { getCommonRichImg(rich) }
        } else {
            charm?.let { getCommonCharmImg(charm) }
        } ?: (0 to null)
    }

    // pair 的 first 是等级   sencond 是背景图片
    fun getRichCharm(sex: Int, rich: Long? = null, charm: Long? = null): Pair<String, Int?> {
        val temppair =
            if (CommonUtils.isMan(sex)) {
                rich?.let { getCommonRichImg(rich) }
            } else {
                charm?.let { getCommonCharmImg(charm) }
            }

        return "LV${temppair?.first}" to temppair?.second
    }

    // pair 的 first 是等级   sencond 是背景图片
    fun getRichCharmLevelInt(sex: Int, rich: Int? = null, charm: Int? = null): Pair<Int, Int?> {
        return if (CommonUtils.isMan(sex)) {
            rich?.let { getCommonRichImg(rich) }
        } else {
            charm?.let { getCommonCharmImg(charm) }
        } ?: (0 to null)
    }

    //返回财富当前等级钻石
    fun getRichNum(richLevel: Long): Int {
        var tempNum = -1

        val richData = getRichData()
        val values = richData.values

        values.forEachIndexed { _, num ->
            if (richLevel >= num) {
                tempNum = num
            }
        }

        return tempNum
    }

    //返回财富等级通用图片
    fun getRichImg(richLevel: Long): Triple<Int, Int, Int> {
        var tempRichLevel = 0
        var tempNum = -1
        var tempNextNum = -1

        val richData = getRichData()
        val values = richData.values

        values.forEachIndexed { index, num ->
            if (richLevel >= num) {
                tempNum = num
                tempNextNum = if (index != values.size - 1) {
                    values.elementAt(index + 1)
                } else {
                    values.elementAt(index)
                }
            }
        }

        richData.forEach { (t, u) ->
            if (tempNum == u) {
                tempRichLevel = t.toInt()
            }
        }

        val tempRichImage = when {
            tempRichLevel in 1 until 10 -> R.drawable.level_wealth1_me
            tempRichLevel in 10 until 21 -> R.drawable.level_wealth1_me
            tempRichLevel in 21 until 41 -> R.drawable.level_wealth6_me
            tempRichLevel in 41 until 61 -> R.drawable.level_wealth12_me
            tempRichLevel in 61 until 81 -> R.drawable.level_wealth18_me
            tempRichLevel in 81 until 100 -> R.drawable.level_wealth24_me
            tempRichLevel in 100 until 101 -> R.drawable.level_wealth24_me
            tempRichLevel in 101 until 121 -> R.drawable.level_wealth30_me
            tempRichLevel in 121 until 141 -> R.drawable.level_wealth31_me
            tempRichLevel in 141 until 161 -> R.drawable.level_wealth32_me
            tempRichLevel in 161 until 181 -> R.drawable.level_wealth33_me
            tempRichLevel in 181 until 200 -> R.drawable.level_wealth34_me
            tempRichLevel >= 200 -> R.drawable.level_wealth34_me
            else -> R.drawable.level_wealth34_me
        }

        if (tempNextNum == -1 && values.isNotEmpty()) {
            tempNextNum = values.elementAt(0)
        }

        return Triple(tempRichLevel, tempNextNum, tempRichImage)
    }

    //返回魅力当前等级钻石
    fun getCharmNum(charmLevel: Long): Int {
        var tempNum = -1

        val charmData = getCharmData()
        val values = charmData.values

        values.forEachIndexed { _, num ->
            if (charmLevel >= num) {
                tempNum = num
            }
        }

        return tempNum
    }

    //返回魅力等级通用图片
    fun getCharmImg(charmLevel: Long): Triple<Int, Int, Int> {
        var tempCharmLevel = 0
        var tempNum = -1
        var tempNextNum = -1

        val charmData = getCharmData()
        val values = charmData.values

        values.forEachIndexed { index, num ->
            if (charmLevel >= num) {
                tempNum = num
                tempNextNum = if (index != values.size - 1) {
                    values.elementAt(index + 1)
                } else {
                    values.elementAt(index)
                }
            }
        }

        charmData.forEach { (t, u) ->
            if (tempNum == u) {
                tempCharmLevel = t.toInt()
            }
        }

        val tempCharmImage = when {
            tempCharmLevel in 1 until 10 -> R.drawable.level_charm1_me
            tempCharmLevel in 10 until 21 -> R.drawable.level_charm1_me
            tempCharmLevel in 21 until 41 -> R.drawable.level_charm5_me
            tempCharmLevel in 41 until 61 -> R.drawable.level_charm10_me
            tempCharmLevel in 61 until 81 -> R.drawable.level_charm15_me
            tempCharmLevel in 81 until 100 -> R.drawable.level_charm16_me
            tempCharmLevel >= 100 -> R.drawable.level_charm16_me
            else -> R.drawable.level_charm16_me
        }

        if (tempNextNum == -1 && values.isNotEmpty()) {
            tempNextNum = values.elementAt(0)
        }

        return Triple(tempCharmLevel, tempNextNum, tempCharmImage)
    }

    //返回视频财富等级通用图片
    fun getLiveRichImg(richLevel: Long): Triple<Int, Int?, Int> {
        var tempRichLevel = -1
        var tempNum = -1

        val richData = getRichData()
        val values = richData.values

        values.forEach {
            if (richLevel >= it) {
                tempNum = it
            }
        }

        richData.forEach { (t, u) ->
            if (tempNum == u) {
                tempRichLevel = t.toInt()
            }
        }

        val (tempRichImage, richColor) = when {
            tempRichLevel in 1 until 21 -> R.drawable.level_wealth1_live to R.color.level_rich1
            tempRichLevel in 21 until 41 -> R.drawable.level_wealth19_live to R.color.level_rich19
            tempRichLevel in 41 until 61 -> R.drawable.level_wealth29_live to R.color.level_rich29
            tempRichLevel in 61 until 81 -> R.drawable.level_wealth39_live to R.color.level_rich39
            tempRichLevel in 81 until 101 -> R.drawable.level_wealth49_live to R.color.level_rich49
            tempRichLevel in 101 until 121 -> R.drawable.level_wealth59_live to R.color.level_rich59
            tempRichLevel in 121 until 141 -> R.drawable.level_wealth69_live to R.color.level_rich69
            tempRichLevel in 141 until 161 -> R.drawable.level_wealth79_live to R.color.level_rich79
            tempRichLevel in 161 until 181 -> R.drawable.level_wealth89_live to R.color.level_rich89
            tempRichLevel in 181 until 200 -> R.drawable.level_wealth99_live to R.color.level_rich89
            tempRichLevel >= 200 -> R.drawable.level_wealth100_live to R.color.level_rich89
            else -> null to R.color.level_rich89
        }

        return Triple(tempRichLevel, tempRichImage, richColor)
    }

    //返回视频魅力等级通用图片
    fun getLiveCharmImg(charmLevel: Long): Triple<Int, Int?, Int> {
        var tempCharmLevel = -1
        var tempNum = -1

        val charmData = getCharmData()
        val values = charmData.values

        values.forEach {
            if (charmLevel >= it) {
                tempNum = it
            }
        }

        charmData.forEach { (t, u) ->
            if (tempNum == u) {
                tempCharmLevel = t.toInt()
            }
        }

        val (tempCharmImage, charmColor) = when {
            tempCharmLevel in 1 until 21 -> R.drawable.level_charm1_live to R.color.level_charm1
            tempCharmLevel in 21 until 41 -> R.drawable.level_charm19_live to R.color.level_charm19
            tempCharmLevel in 41 until 61 -> R.drawable.level_charm29_live to R.color.level_charm29
            tempCharmLevel in 61 until 80 -> R.drawable.level_charm39_live to R.color.level_charm39
            tempCharmLevel >= 80 -> R.drawable.level_charm49_live to R.color.level_charm49
            else -> null to R.color.level_charm1
        }

        return Triple(tempCharmLevel, tempCharmImage, charmColor)
    }

    //获取通用财富值集合
    private fun getRichData(): MutableMap<String, Int> {
        val richLevel = AppConfigManager.getConfig(CommonConst.Configs.richLevel)
        val map = mutableMapOf<String, Int>()
        val temp = richLevel?.replace("[{}]".toRegex(), "")
        val replace = temp?.replace("\"", "")
        val keyValue = replace?.split(",")
        keyValue?.forEach {
            val pairs = it.split(":")
            if (pairs.size == 2) {
                map[pairs[0]] = pairs[1].toInt()
            }
        }
        return map
    }

    private fun getCharmData(): MutableMap<String, Int> {
        val charmLevel = AppConfigManager.getConfig(CommonConst.Configs.charmLevel)
        val map = mutableMapOf<String, Int>()
        val temp = charmLevel?.replace("[{}]".toRegex(), "")
        val replace = temp?.replace("\"", "")
        val keyValue = replace?.split(",")
        keyValue?.forEach {
            val pairs = it.split(":")
            if (pairs.size == 2) {
                map[pairs[0]] = pairs[1].toInt()
            }
        }
        return map
    }

    private var richNicknameColor: MutableMap<String, String>? = null
    private var richMessageColor: MutableMap<String, String>? = null
    private var richData: MutableMap<String, Int>? = null

    const val NAME = 1  //名称
    const val MESSAGE = 2  //字体

    @IntDef(
        NAME,
        MESSAGE,
    )
    annotation class TextColorType

    /**
     * 缓存生成了颜色的用户
     */
    private var vipColorCache: MutableMap<String, String>? = null

    /**
     * 获取vip颜色
     */
    fun getVipColorByLevel(level: Long?): Int? {
        if (level == null) return null
        if (vipColorCache == null) {
            vipColorCache = AppConfigManager.getConfig(CommonConst.Configs.vipColor)
                ?.let { JsonUtils.fromJson<MutableMap<String, String>>(it) }
        }
        return strToHexColor(vipColorCache?.get("$level"))
    }

    /**
     * 缓存生成了颜色的用户
     */
    private var colorCache = ConcurrentHashMap<Long, UserAttireCacheBean>()

    /**
     * 根据豪等級获取名字变色色值
     */
    fun getNameColorByRichLevel(userId: Long?, rich: Long?, defaultColor: Int): Int {
        var oldUser: UserAttireCacheBean? = null
        if (userId != null) {
            val cache = colorCache[userId]
            if (cache?.nameColor != null && (rich == cache.rich || rich == null)) {
                return cache.nameColor ?: defaultColor
            }
            oldUser = cache
        }
        if (rich == null || rich == 0L) {
            return defaultColor
        }
        if (richNicknameColor == null) {
            richNicknameColor = AppConfigManager.getConfig(CommonConst.Configs.richNicknameColor)
                ?.let { JsonUtils.fromJson<MutableMap<String, String>>(it) }
        }
        val color = richNicknameColor?.let { colors ->
            if (richData == null) {
                richData = getRichData()
            }
            richData?.let { richs ->
                var tempRichLevel = -1
                var tempNum = -1
                val values = richs.values
                values.forEach {
                    if (rich >= it) {
                        tempNum = it
                    }
                }
                richs.forEach { (t, u) ->
                    if (tempNum == u) {
                        tempRichLevel = t.toInt()
                    }
                }
                val color = strToHexColor(colors[tempRichLevel.toString()])
                userId?.let {
                    colorCache[it] =
                        UserAttireCacheBean(it, rich, tempRichLevel, oldUser?.messageColor, color)
                }
                color
            }

        } ?: defaultColor
        return color
    }

    /**
     * 根据豪等級获取名字变色色值
     */
    fun getTextColorByRichLevel(userId: Long?, rich: Long?, defaultColor: Int): Int {
        var oldUser: UserAttireCacheBean? = null
        if (userId != null) {
            val cache = colorCache[userId]
            if (cache?.messageColor != null && (rich == cache.rich || rich == null)) {
                return cache.messageColor ?: defaultColor
            }
            oldUser = cache
        }
        if (rich == null || rich == 0L) {
            return defaultColor
        }
        if (richMessageColor == null) {
            richMessageColor = AppConfigManager.getConfig(CommonConst.Configs.richMessageColor)
                ?.let { JsonUtils.fromJson<MutableMap<String, String>>(it) }
        }
        val color = richMessageColor?.let { colors ->
            if (richData == null || rich == 0L) {
                richData = getRichData()
            }
            richData?.let { richs ->
                var tempRichLevel = -1
                var tempNum = -1
                val values = richs.values
                values.forEach {
                    if (rich >= it) {
                        tempNum = it
                    }
                }
                richs.forEach { (t, u) ->
                    if (tempNum == u) {
                        tempRichLevel = t.toInt()
                    }
                }
                val color = strToHexColor(colors[tempRichLevel.toString()])
                userId?.let {
                    colorCache[it] =
                        UserAttireCacheBean(it, rich, tempRichLevel, color, oldUser?.nameColor)
                }
                color
            }
        } ?: defaultColor
        return color
    }

    fun serColor2HexColor(color: String?): Int? {
        return strToHexColor(color)
    }

    /**
     *@color:后台返回的色值
     *@defaultColor:ui默认的色值
     */
    private fun strToHexColor(color: String?): Int? {
        val validColorRegex = Regex("^#[0-9A-Fa-f]{6}$|^#[0-9A-Fa-f]{8}$")
        return when {
            color.isNullOrBlank() -> null
            validColorRegex.matches(color) -> Color.parseColor(color)
            else -> null
        }
    }

}